package finalcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JTextArea;

public class TCPSocketClientremote {

	/**
	 * The client host name.
	 */
	public String my_serverHost;

	/**
	 * The client port.
	 */
	public int my_serverPort;

	public boolean play_mode = false;

	/**
	 * Sets the client up with the server details.
	 * 
	 * @param the_serverHost
	 *            the server host name.
	 * @param the_serverPort
	 *            the server port.
	 */

	public Vector<String> movieList; //The returned list of media files


	public TCPSocketClientremote(final String the_serverHost, final int the_serverPort) {
		my_serverHost = the_serverHost;
		my_serverPort = the_serverPort;
	}

	/**
	 * Creates a connection to the server and sends a message.
	 * 
	 * @param a_message
	 *            the message to send to the server.
	 * @return 
	 */
	public boolean sendMessage(final String a) {
		try {
			// Create a connection to the server.
			final Socket toServer = new Socket(my_serverHost, my_serverPort);
			final ListParser LP = new ListParser(); 
			final PrintWriter out = new PrintWriter(toServer.getOutputStream(), true);

			// Write the message to the socket.
			out.println(a);

			final BufferedReader in = new BufferedReader(new InputStreamReader(toServer.getInputStream()));

			//upon receiving "list" transmits a vector of strings with each string being a media file
			if (a.equalsIgnoreCase("list")) {
				movieList = new Vector<String>();
				final String msg = in.readLine();
				movieList = LP.MakeList(msg);
				System.out.println(msg);

			} else if (a.split("->")[0].equalsIgnoreCase("play")) {
				final String msg = in.readLine();
				if (msg.split("->")[0].equalsIgnoreCase("playing")) {
					play_mode = true;
				}
			} else {
				final String msg = in.readLine();
			}

		} catch (final IOException ioe) {
			return false;
		} catch (final SecurityException se) {
			se.printStackTrace();
		}
		return true;
	}

} // end class

